const app = getApp()
var common = require("../../common.js")
Page({
  onShow: function () {},
  data: {
    album: ['', '', '', '', '', ''],
    uploadPhotoDisable:false,
    showTip: false,
    qa: [
      { q: '怎么设置呀', a: '第一步：在查找对象中输入希望指认的对象称呼，默认为您本人。第二步：点击“上传照片”按钮，上传1至6张想要好友查找的对象照片。第三步：点击“下步操作”进入下级操作界面。' }, 
      { q: '为什么要上传多张图片呢', a: '好友参与时，我们会随机从您上传的图片中取出一张，并结合我们预设的5张图片，供您的好友选择，你预设的图片多些，可以降低好友作弊的可能唷。' }, 
      { q: '我上传的图片完全吗？', a: '我们向您保证，你上传的图片只会在你与好友的游戏中出现，我们不会挪作他用。' }
    ]
  },
  //处理图片显示
  processPhoto:function(){
    var album = ['', '', '', '', '', ''];
    var photoNum = app.globalData.album.length;
    for (var i = 0; i < photoNum; i++) {
        album[i] = app.globalData.album[i];
    }
    this.setData({
      album: album
    })
  },
  /**
     * 弹出层函数
     */
  //出现
  showTip: function () {
    this.setData({ showTip: true })
  },
  //消失
  hideTip: function () {
    this.setData({ showTip: false })
  },
  onLoad: function () {   
    this.setData({
      nickName: app.globalData.userInfo.nickName
    })
  },
  //上传图片
  upload:function(){
    var that=this;    
    var imgCount = 6 - app.globalData.album.length;
    if (imgCount<=0){
      wx.showToast({
        title: "只可以上传6张图唷",
        icon: 'none'
      })
      return;
    }
    wx.chooseImage({
      count: imgCount,
      sizeType:['compressed'],
      success: function (res) {
        wx.showLoading({
          title: '正在上传',
		      mask:true,
        })
        var tempFilePaths = res.tempFilePaths;
        for (var index in tempFilePaths){
          that.uploadOne(tempFilePaths[index]);     
        }
      }
    })
  },
  uploadOne:function(onePic){
    var that=this;
    wx.uploadFile({
      url: app.buildURL('c=Index&a=ajaxUpload&tp=user'), //仅为示例，非真实的接口地址
      filePath: onePic,
      name: 'file',
      success: function (res) {
        if (typeof res == 'object') {
          var ret = JSON.parse(res.data);
        } else {
          var unit8Arr = new Uint8Array(res.data.data)
          var encodedString = String.fromCharCode.apply(null, unit8Arr),
            decodedString = decodeURIComponent(escape((encodedString)));//没有这一步中文会乱码
          var ret = JSON.parse(decodedString);
        }
        wx.showToast({
          title: ret.msg,
          icon: 'none'
        })
        if (ret.status == 1) {
          return false;
        }

        var photoNum = app.globalData.album.length;
        var append = true
        for (var i = 0; i < photoNum; i++) {
          if (app.globalData.album[i].length < 5) {
            app.globalData.album[i] = ret.pic;
            append = false;
            break;
          }
        }
        if (append) {
          app.globalData.album.push(ret.pic);
        }
        that.processPhoto()
        if (app.globalData.album.length >= 6) {
          that.setData({
            uploadPhotoText: '照片数量已达上限',
            uploadPhotoDisable: !that.data.uploadPhotoDisable
          })
        }
      },
      complete: function (res) {
        wx.hideLoading();
      }
    });     
  },
  //删除图片
  deletePhoto:function(e){
    var that=this;
    if(e.target.id!='remove'){
      return false
    }
    var src=e.target.dataset.src;
    var len = that.data.album.length;
    for (var i = 0; i < len; i++) {
      if (that.data.album[i] == src) {
        that.data.album.splice(i, 1);    
        app.globalData.album.splice(i, 1);    
        that.processPhoto();
        break;
      }
    }
    
  },
  //下一步，跳转充值页面
  nextStep: function (e) {
    var formId = e.detail.formId;
    let target = e.detail.value.target.trim()
    if (target.length<1){
      wx.showToast({
        title: '请指定查找对象',
        image:'../../image/warn.png'
      });
      return false;
    }
    if (app.globalData.album.length<1){
      wx.showToast({
        title: '请上传图片',
        image: '../../image/warn.png'
      });
      return false;
    }

    wx.showLoading({
          title: '正在保存...',
    })

    var that = this;
    common.request({
      url: 'c=Game&a=editGame',
      data: {
        formId: formId,
        target:target,
        album: app.globalData.album.join(','),
      },
      success: function (e) {
        if(e.status==0){
          wx.navigateTo({
            url: '../../pages/suren/money?gid='+e.gid,
          })
        } else if (e.status == 1){
          wx.showModal({
            title: '提示',
            content: e.msg,
            success: function (res) {
              if (res.confirm) {
                wx.navigateTo({
                  url: '../../pages/suren/home',
                })
              } else if (res.cancel) {
                console.log('用户点击取消')
              }
            }
          })

        } else{
          wx.showModal({
            title: '错误',
            content: e.msg,
          })
        }
      },
      fail: function (e) {
        console.log(e)
      },
	  complete:function(){
		  wx.hideLoading();
	  },
      method: "POST"
    });

    console.log(e.detail.value.target);
    return;



  },


  
})
